package com.cgtong.android.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.cgtong.android.App;
import com.cgtong.android.R;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.db.UserAccount;
import com.cgtong.android.mvp.contract.SignContract;
import com.cgtong.android.mvp.presenter.SignPresenter;
import com.cgtong.android.util.CommonUtils;
import com.cgtong.android.util.PostUtil;
import com.cgtong.android.widget.SignCalendar;
import com.kingja.loadsir.callback.SuccessCallback;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;


public class SignActivity extends BaseTitleActivity implements SignContract.View {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_sign)
    TextView tv_sign;
    @BindView(R.id.calendar)
    SignCalendar calendar;
    @BindView(R.id.tv_signnum)
    TextView tv_signnum;
    @BindView(R.id.iv_head)
    CircleImageView iv_head;

    private List<String> list = new ArrayList<String>(); //设置标记列表

    private SignPresenter signPresenter;
    private SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private String monthStr;
    private Date currDate = new Date();
    private String nowDate;
    private String nowMonth;
    private UserAccount account;

    protected void sign() {
        signPresenter.sign();
    }

    protected void lastMonth() {
        calendar.lastMonth();
    }

    protected void nextMonth() {
        calendar.nextMonth();
    }

    @Override
    protected String getContentTitle() {
        return getString(R.string.txt_sign);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_sign;
    }

    @Override
    protected void loadDataByNet() {

    }

    @Override
    protected void onDataReloadByNet(View v) {

    }

    private void initView() {
        signPresenter = new SignPresenter(this);
        tv_title.setText(calendar.getCalendarYear() + "年" + calendar.getCalendarMonth() + "月");
        //监听当前月份
        calendar.setOnCalendarDateChangedListener(new SignCalendar.OnCalendarDateChangedListener() {
            public void onCalendarDateChanged(int year, int month) {
                tv_title.setText(year + "年" + month + "月");
                monthStr = year + "-" + (month < 10 ? "0" + month : month);
                initData(monthStr);
            }
        });

        account = App.getInstance().getCurrentAccount();
        if (account != null) {
            CommonUtils.setUserAvatar(this, iv_head, account.getAvatarId());
        }
    }

    public void initData(String date) {
        //获取当前月份签到
        signPresenter.getSignInfo(date.replace("-", ""));
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        nowDate = dateFormat.format(currDate);
        nowMonth = monthFormat.format(currDate);
        monthStr = monthFormat.format(currDate);
        initData(monthStr);
    }

    @OnClick({R.id.title_left_txt, R.id.tv_sign, R.id.iv_left, R.id.iv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_txt:
                finish();
                break;
            case R.id.tv_sign:
                sign();
                break;
            case R.id.iv_left:
                lastMonth();
                break;
            case R.id.iv_right:
                nextMonth();
                break;
            default:
                break;
        }
    }

    @Override
    public void signSuccess() {
        ToastUtils.showShort("签到成功");
        initData(monthStr);

        tv_sign.setText("今日已签");
        tv_sign.setEnabled(false);
    }

    @Override
    public void signFail(String msg) {
        ToastUtils.showShort(msg);
    }

    @Override
    public void getSignList(List<String> datas) {
        PostUtil.postCallbackDelayed(loadService, SuccessCallback.class);
        int sumSign = 0;
        for (String str : datas) {
            String data = monthStr + "-" + str;
            list.add(data);

            if (monthStr.equals(nowMonth)) {
                sumSign++;
            }

            if (data.equals(nowDate)) {//当天已签到
                tv_sign.setText("今日已签");
                tv_sign.setEnabled(false);
            }
        }
        calendar.addMarks(list, 0);

        if (monthStr.equals(nowMonth)) {
            tv_signnum.setText(getString(R.string.txt_sign_pile, sumSign));
        }
    }
}
